package com.xx.doublexchat.common.websocket.utils;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * Netty工具类
 */
public class NettyUtils {

    public static AttributeKey<String> TOKEN = AttributeKey.valueOf("token");
    public static AttributeKey<String> IP = AttributeKey.valueOf("ip");
    public static AttributeKey<Long> UID = AttributeKey.valueOf("uid");

    /**
     * 设置token
     * @param channel
     * @param key
     * @param value
     * @param <T>
     */
    public static <T> void setAttr(Channel channel, AttributeKey<T> key,T value) {
        Attribute<T> attr = channel.attr(key);
        attr.set(value);
    }

    /**
     * 获取token
     *
     * @param channel
     * @param key
     * @param <T>
     * @return
     */
    public static <T> T getAttr(Channel channel, AttributeKey<T> key) {
        Attribute<T> attr = channel.attr(key);
        return attr.get();
    }
}
